<template>
  <el-container class="home-container">
    <!-- 头部区域-->
    <el-header>
      <div>
        <img src="../assets/logo.png"
             alt />
        <span>后台管理项目</span>
      </div>
      <el-button type="info"
                 @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域-->
      <el-aside :width="flag?'64px':'200px'">
        <div class="toggle-button"
             @click="toggleSize">|||</div>
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 unique-opened
                 :collapse="flag"
                 :collapse-transition="false"
                 router
                 :default-active="active">
          <!-- 一级菜单栏 -->
          <el-submenu :index="item1.id+''"
                      v-for="item1 in muneList"
                      :key="item1.id">
            <template slot="title">
              <!-- 一级菜单栏图标 -->
              <i :class="iconList[item1.id]"></i>
              <!-- 一级菜单栏名称 -->
              <span>{{item1.authName}}</span>
            </template>

            <!-- 二级菜单栏 -->
            <el-menu-item :index="'/'+item2.path"
                          v-for="item2 in item1.children"
                          :key="item2.id"
                          @click="itemActivePath('/'+item2.path)">
              <template slot="title">
                <!-- 二级菜单栏图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单栏文字 -->
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      muneList: '',
      iconList: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 判断是否折叠
      flag: false,
      // 当前被激活的链接
      active: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取列表失败')
      this.muneList = res.data
    },
    toggleSize () {
      this.flag = !this.flag
    },
    itemActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created () {
    this.getMenuList()
    this.active = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scope>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  color: #ffffff;
  > div {
    display: flex;
    align-items: center;
    height: 100%;
    > img {
      height: 100%;
      margin-right: 20px;
    }
  }
}

.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-aside .el-menu {
  border-right: none;
}
.toggle-button {
  text-align: center;
  color: #fff;
}
</style>
